﻿namespace EasyGo.Controllers
{
    using System.Web.Mvc;
    using EasyGo.Common.Commands;
    using EasyGo.Common.Queries;
    using EasyGo.Logic;
    using EasyGo.Models;
    using EasyGo.Utils;

    public class ProjectsController : EasyGoJsonController
    {
        private readonly IProjectManager projectManager;

        private readonly IProjectQueries projectQueries;

        public ProjectsController(IProjectManager projectManager, IProjectQueries projectQueries)
        {
            this.projectManager = projectManager;
            this.projectQueries = projectQueries;
        }

        public virtual ActionResult GetAll()
        {
            var response = new JsonResponse(true, this.projectQueries.GetAllProjects());
            return response.ToActionResult();
        }

        public virtual ActionResult Find(int id)
        {
            var response = new JsonResponse();
            var project = this.projectQueries.FindProject(id);
            if (project != null)
            {
                response.Response = project;
            }
            else
            {
                response.Fail("Project not found");
            }

            return response.ToActionResult();
        }

        [HttpPost]
        public virtual ActionResult Add(EditProjectCommand command)
        {
            var response = new JsonResponse(true);
            this.projectManager.CreateProject(command);
            return response.ToActionResult();
        }

        [HttpPost]
        public virtual ActionResult Modify(EditProjectCommand command)
        {
            var response = new JsonResponse(true);
            this.projectManager.EditProject(command);
            return response.ToActionResult();
        }

        [HttpPost]
        public virtual ActionResult Delete(int id)
        {
            var response = new JsonResponse(true);
            this.projectManager.DeleteProject(id);
            return response.ToActionResult();
        }

        public virtual ActionResult Lookup(string term)
        {
            var response = new JsonResponse(true, this.projectQueries.GetLookup(term));
            return response.ToActionResult();
        }
    }
}
